import { Form, Input, Button } from 'antd';
import Switch from "../../base/Switch/index";
import RegionLeveChose from "../../base/RegionLeveChose/index";

import style from "./index.module.scss";
import {addNewAddress} from "../../serverAPI/index";

import {address} from "../../common/data/_dataStructure.js";
const layout = {
    labelCol: {
      span: 8,
    },
    wrapperCol: {
      span: 16,
    },
  };

const tailLayout = {
    wrapperCol: {
        offset: 8,
        span: 16,
    },
};

const isDefault = {
  isDefault: 0
}
const setDefault = (checked)=> {
  isDefault.isDefault = checked;
}
let location = {};
const setLocation = (values)=>{
  location = {...values,...isDefault};
  // console.log("location",location);
}

const newAddressForm = (react) => {
  
    const onFinish = (values) => {
      let addressInfo = Object.assign({},address,values,location,isDefault);

      let temp = localStorage.getItem("loginUserInfo");
      if(!temp) this.props.history.push("/login");
      
      let userInfo = JSON.parse(temp);
      addressInfo.userID = userInfo.id;

      addNewAddress(addressInfo,(res)=>{
        if(res.code==="01"){
           alert('添加成功');
            react.props.history.push("/useraddress");
        }else{
          alert(res.msg);
        }
      });
    };
  
    const onFinishFailed = (errorInfo) => {
      console.log("errorInfo",errorInfo);
      react.setState({formErrorInfo:errorInfo, isformFill:false},()=>{
        console.log(react.state.addressInfo);
      })
    };
  
    return (
      <Form
        {...layout}
        name="basic"
        initialValues={{
          remember: true,
        }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
      >

        <ul className={style["user-info-form"]}>
          <li><b>收货人姓名</b>
              <Form.Item name="realname"  rules={[{required: true, message: '输入姓名!'}]}>
                  <Input placeholder="请输入" />
              </Form.Item>
          </li>
          
          <li><b>手机号码</b>
              <Form.Item name="phonenum"  rules={[{required: true, message: '输入电话!'}]}>
                  <Input placeholder="请输入" />
              </Form.Item>
          </li>

          <li><b>所在地区</b>
              <RegionLeveChose change={setLocation} placeholder="请选择" />
          </li>

          <li><b>详细地址</b>
              <Form.Item name="community"  rules={[{required: true, message: '输入详细地址!'}]}>
                  <Input placeholder="请输入" />
              </Form.Item>
          </li>
          
          <li><b>设为默认地址</b>
            <Switch className={style["isDefault-checked"]} change = {setDefault}></Switch>
          </li>
        </ul>
        
        <div className={style["submit-btn-position"]}>
          <Form.Item {...tailLayout}>
              <Button type="primary" className={style["submit-btn"]} htmlType="submit">
                  确定
              </Button>
          </Form.Item>
        </div>
      </Form>
    );
  };

  export default newAddressForm;